#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright @ 2014 OPS, Qunar Inc. (qunar.com)
#
# Author: tingfang.bao <tingfang.bao@qunar.com>
# DateTime: 14-12-22 下午5:38
from logging import FileHandler
import logging
from qlib.common import log

from flask import Flask, redirect, session, g, current_app

LOG = logging.getLogger(__name__)

app = Flask(__name__, instance_path='/home/skyfree/test')
app.secret_key = "1234"


@app.route("/")
def index():
    print 'abc'
    # session['mobile'] = '13810963365'
    print current_app.name
    g.comment = 'come here!'
    LOG.warn("testabc")
    # return render_template("index.html", name="<script>alert('ok');</script>", data={"type": "server"})
    return redirect("/faq")


@app.route("/faq")
def faq():
    return 'haha'

@app.teardown_request
def f(exception=None):
    print 'bye'

def main():
    print app.root_path
    app.debug = False

    file_handler = FileHandler("log")
    file_handler.setLevel(logging.WARNING)
    file_handler.setFormatter(logging.Formatter("""
Message type: %(levelname)s
Location: %(pathname)s: %(lineno)d
Module: %(module)s
Function: %(funcName)s
Time: %(asctime)s
Message:
%(message)s
"""))
    app.logger.addHandler(file_handler)

    app.run()


if __name__ == '__main__':
    # with app.test_request_context("/", method="POST"):
    # resp = make_response(index())
    # resp.set_cookie("username", "baotingfang")
    # print resp
    # with app.test_client() as client:
    # with client.session_transaction() as se:
    # se['mobile'] = 'xxxxxxx'
    #
    #     rv = client.get('/', follow_redirects=True)
    #     print session['mobile']
    #     print rv.data
    with app.open_instance_resource('data', mode='r') as f:
        print f.read()
    main()
